import axios from 'axios'
import {Message, MessageBox} from 'element-ui'
import {getToken, setToken} from '@/utils/auth'
import router from '@/router'
import Tools from '@/utils/tools'


//设置axios为form-data
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.transformRequest = [function (data) {
//   let ret = ''
//   for (let it in data) {
//     ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
//   }
//   return ret
// }]

// 创建axios实例
const service = axios.create({
  baseURL: Tools.isDev() ? 'http://127.0.0.1:2001' : '/hpApp' // api的base_url
  // timeout: 15000                  // 请求超时时间2
});

function errorMsg(status, msg) {
  status = parseInt(status);
  let result = '';
  switch (status) {
    case 400:
      result = msg ? msg : '请求错误';
      break;
    case 401:
      result = msg ? msg : '未授权，请登录';
      break;
    case 403:
      result = msg ? msg : '拒绝访问';
      break;
    case 404:
      result = msg ? msg : `请求地址出错: ${error.response.config.url}`;
      break;
    case 408:
      result = msg ? msg : '请求超时';
      break;
    case 500:
      result = msg ? msg : '服务器内部错误';
      break;
    case 501:
      result = msg ? msg : '服务未实现';
      break;
    case 502:
      result = msg ? msg : '网关错误';
      break;
    case 503:
      result = msg ? msg : '服务不可用';
      break;
    case 504:
      result = msg ? msg : '网关超时';
      break;
    case 505:
      result = msg ? msg : 'HTTP版本不受支持';
      break;
    default:
      result = msg;
  }
  return result;
}

// service.defaults.crossDomain = true;
// service.defaults.withCredentials = true;
// request拦截器
service.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Bearer ' + getToken();
  config.headers['Access-Control-Allow-Origin'] = '*';
  return config
}, error => {
  Promise.reject(error)
});
// respone拦截器
service.interceptors.response.use(
  response => {
    const res = response.data;
    // 200-请求处理成功
    if (res.code === 200) {
      return res;
    } else if (res.code === 999) {
      Message({
        message: '您已经退出登录',
        type: 'success',
        duration: 3 * 1000
      });
      router.push('/login');
      return res;
    } else {
      Message({
        message: errorMsg(res.result, res.message),
        type: 'error',
        duration: 3 * 1000
      });
      if (res.code === 2000) {
        setToken('');
        router.push('/login');
      }
      return Promise.reject(res);
    }
  },
  error => {
    console.info(error)
    if (error && error.response) {

      let msg = error.response.data ? error.response.data.data : '';
      error.message = errorMsg(error.response.status, msg);
    } else
      error.message = '连接错误，请联系管理员检查后台服务是否启动';
    Message({
      message: error.message,
      type: 'error',
      duration: 3 * 1000
    });

    return Promise.reject(error)
  }
);
export default service

